package com.zhengsr.latte.ec.ec.lunch;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;

import com.zhengsr.latte.ec.R;
import com.zhengsr.latte.utils.SprefUtils;

import java.text.MessageFormat;

/**
 * Created by zhengshaorui on 2018/4/20.
 */

public class LunchActivity extends BaseLunchActivity {
    private static final String TAG = "LunchActivity";

    private static final int MSG_TIME_COUNT = 1;
    private    int COUNT = 6;
    private AppCompatTextView mTimeView;
    /**
     * handler
     */
    Handler mHandler = new Handler(Looper.myLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == MSG_TIME_COUNT){
                if (mTimeView != null){
                    COUNT -- ;
                    mTimeView.setText(MessageFormat.format("跳过\n{0}s",COUNT));
                    if (COUNT <= 0){
                        checkIsFristLunch();
                        mHandler.removeMessages(MSG_TIME_COUNT);
                    }else{
                        mHandler.sendEmptyMessageDelayed(MSG_TIME_COUNT,1000);
                    }
                }
            }
        }
    };

    private void checkIsFristLunch() {
        boolean isSave = (boolean) SprefUtils.getSprefValue(SprefUtils.ISLUNCH, SprefUtils.SprefType.BOOLEAN);
        //检查是否已经登录过
        if (!isSave){
            SprefUtils.saveSprefValue(SprefUtils.ISLUNCH,true);
            //没有则执行 splash
            getSupportDelegate().start(new SplashActivity(),SINGLETASK);
        }else{
            //有则检查用户是否登录
            checkUserSignIn();
        }
        mHandler.removeMessages(MSG_TIME_COUNT);
    }


    @Override
    public Object getLayout() {
        return R.layout.delegate_lunch;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState, View view) {
        mTimeView = view.findViewById(R.id.lunch_text);
        mTimeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkIsFristLunch();
            }
        });

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mHandler.sendEmptyMessage(MSG_TIME_COUNT);
    }





    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeMessages(MSG_TIME_COUNT);

    }
}
